package com.fzy.controller.system;


import com.fzy.common.ResponseResult;
import com.fzy.common.code.UserCode;
import com.fzy.system.domain.User;
import com.fzy.system.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/users")
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping
    public ResponseResult addUser(@RequestBody User user) {
        System.out.println("user add success");
        boolean flag = userService.save(user);
        return new ResponseResult(flag ? UserCode.SAVE_OK : UserCode.SAVE_ERR, "add添加用户");
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id) {
        boolean flag = userService.removeById(id);
        return new ResponseResult(flag ? UserCode.DELETE_OK : UserCode.DELETE_ERR, "删除用户操作");
    }


    @PutMapping
    public ResponseResult updateUser(@RequestBody User user) {
        boolean flag = userService.updateById(user);
        return new ResponseResult(flag ? UserCode.UPDATE_OK : UserCode.UPDATE_ERR, "更新用户信息");
    }

    @GetMapping("/{id}")
    public ResponseResult getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        Integer integer = user != null ? UserCode.GET_OK : UserCode.GET_ERR;
        String msg = user != null ? "查询单个成功" : "数据查询失败";
        return new ResponseResult(integer, msg, user);
    }


}
